/**
 * D5Power Studio FPower 2D MMORPG Engine
 * 第五动力FPower 2D 多人在线角色扮演类网页游戏引擎
 * 
 * copyright [c] 2010 by D5Power.com Allrights Reserved.
 */ 
package com.D5Power.Objects
{
	
	import com.D5Power.Controler.BaseControler;
	import com.D5Power.display.D5TextField;
	import com.D5Power.utils.XYArray;
	
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * 游戏角色类对象基类
	 * 游戏中全部角色类（包括NPC）的根类
	 */
	
	public class CharacterObject extends ControlActionObject implements IName
	{
		/**
		 * 名称显示缓冲
		 */ 
		protected var _nameBuffer:BitmapData;
		/**
		 * 名称的显示位置
		 */ 
		protected var _nameBufferPos:XYArray;
		
		public function CharacterObject(ctrl:BaseControler=null)
		{
			//TODO: implement function
			super(ctrl);
			objectName = 'CharacterObject';
			_directionNum = 4;
		}
		
		/**
		 * 名称缓冲
		 */ 
		public function get nameBuffer():BitmapData
		{
			return _nameBuffer;
		}
		
		/**
		 * 名称的显示位置
		 */ 
		public function get namePos():XYArray
		{
			return _nameBufferPos;
		}
		
		public function get width():uint
		{
			return _graphics.frameWidth
		}
		
		public function get height():uint
		{
			return _graphics.frameHeight;
		}
		
		/**
		 * 设置名字
		 * @param	_name	角色名
		 * @param	color	字体颜色	，若为-1则自动根据Global的阵营设置进行判断
		 * @param	bordercolor	描边颜色
		 */ 
		public function setName(_name:String,color:int=-1,bordercolor:int=0):void
		{
			var text:D5TextField = new D5TextField('',0xFFFFFF);
			
			text.text = _name;
			text.autoGrow();
			
			if(color==-1)
			{
				text.textColor = Global.camp==camp ? 0x99ff00 : 0xff0000;
				text.fontBorder = Global.camp==camp ? 0x003300 : 0x390000;
			}else{
				text.textColor = color;
				text.fontBorder = bordercolor;
			}
			text.align=D5TextField.CENTER;
			
			if(_nameBuffer!=null)
			{
				_nameBuffer.dispose();
				_nameBuffer = null;
			}
			
			_nameBufferPos = new XYArray(int((graphics.frameWidth-text.width)/2),graphics.frameHeight);
			if(graphics.frameWidth==0)
			{
				var timer:Timer = new Timer(500);
				timer.addEventListener(TimerEvent.TIMER,build);
				timer.start();
			}

			_nameBuffer = new BitmapData(text.width,text.height,true,0x00000000);
			_nameBuffer.draw(text);
			
			text = null;
		}
		
		protected function build(e:TimerEvent):void
		{
			var target:Timer = e.target as Timer;
			if(graphics.frameWidth>0)
			{
				target.stop();
				target.removeEventListener(TimerEvent.TIMER,build);
				target = null;
				
				_nameBufferPos = new XYArray(int((graphics.frameWidth-_nameBuffer.width)/2),graphics.frameHeight);
			}
		}
		
	}
	
}